# 设置日志记录器
import logging
from typing import Dict, Any, Optional

from config.logging_config import get_logger


class LoggerUtils:
    """日志记录相关的通用方法集合"""

    @staticmethod
    def setup_logger(name: str, level: int = logging.INFO) -> logging.Logger:
        """获取日志记录器（使用全局配置）"""
        # 直接返回全局配置的logger，不再重新配置
        logger = get_logger(name)

        # 可以设置特定模块的日志级别，但不添加新的handler
        if level != logging.INFO:
            logger.setLevel(level)

        return logger

    @staticmethod
    def log_agent_start(agent_name: str, input_text: str, logger: Optional[logging.Logger] = None):
        """记录Agent开始处理的日志"""
        if logger is None:
            logger = get_logger(__name__)
        logger.info(f"{agent_name}开始处理: {input_text[:50]}{'...' if len(input_text) > 50 else ''}")

    @staticmethod
    def log_agent_complete(agent_name: str, response_length: int, success: bool = True,
                           logger: Optional[logging.Logger] = None):
        """记录Agent完成处理的日志"""
        if logger is None:
            logger = get_logger(__name__)
        status = "成功" if success else "失败"
        logger.info(f"{agent_name}处理{status}，响应长度: {response_length}")

    @staticmethod
    def log_tool_execution(tool_name: str, success: bool, result_length: int = 0,
                           logger: Optional[logging.Logger] = None):
        """记录工具执行日志"""
        if logger is None:
            logger = get_logger(__name__)
        status = "成功" if success else "失败"
        logger.info(f"🔧 工具执行 - {tool_name}: {status}, 结果长度: {result_length}")

    @staticmethod
    def log_memory_operation(operation: str, success: bool, logger: Optional[logging.Logger] = None):
        """记录记忆操作日志"""
        if logger is None:
            logger = get_logger(__name__)
        status = "成功" if success else "失败"
        logger.info(f"🧠 记忆操作 - {operation}: {status}")

    @staticmethod
    def log_error(error_msg: str, exception: Optional[Exception] = None,
                  logger: Optional[logging.Logger] = None):
        """记录错误日志"""
        if logger is None:
            logger = get_logger(__name__)

        if exception:
            logger.error(f"❌ 错误: {error_msg}", exc_info=exception)
        else:
            logger.error(f"❌ 错误: {error_msg}")

    @staticmethod
    def log_performance(operation: str, duration: float, logger: Optional[logging.Logger] = None):
        """记录性能日志"""
        if logger is None:
            logger = get_logger(__name__)
        logger.info(f"⏱️ 性能 - {operation}: {duration:.2f}秒")
